package org.example.controller;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.bean.vo.MerchantMarketErrorResult;
import org.example.bean.vo.MerchantMarketErrorVO;
import org.example.dao.mapper.DynamicKpiColumnMapper;
import org.example.dao.mapper.DynamicViewConfigMapper;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "/test")
public class TestController {

    private final DataSource dataSource;
    private final DynamicViewConfigMapper dynamicViewConfigMapper;

    @GetMapping("/test3")
    public Object test3() {
        String sql = "/**priority=p3**/select distinct \"merchant_id\" as \"merchant_id\" from dim.dim_merchant  where  DATE ( date( register_dt ))  between date_add('month', -1, date_trunc('month', current_date)) and date_add('day', -1 ,date_trunc('month', current_date)) AND \"merchant_status_name\" = '启用' AND \"merchant_channel_name\" <> '京东'";
        sql = sql.replace("'", "''"); // 替换单引号

        String insertSql = "INSERT INTO `merchant_data`.`complex_sql_test` (`activity_type`, `activity_name`, `old_json`, `old_sql`, `new_json`, `new_sql`) VALUES ('market_activity', '%s', '%s','%s', NULL, NULL);";
        String sql2 = String.format(insertSql, "name", null, sql);
        System.out.println(sql2);
        dynamicViewConfigMapper.explainQuery(sql2);
        return "SUS";
    }

    private static String trimSql(String sql) {
        sql = sql.replace("'", "''"); // 替换单引号
        return sql.trim();
    }

    @GetMapping("/test1")
    public Object test1() {
        MerchantMarketErrorVO model = new MerchantMarketErrorVO();
        model.setBatchId("D02B9F60E2F744B3AAFD977CCF2642CB");
        model.setReason("111");

        for (int i = 0; i < 20 * 10000; i++) {
            model.getMerchantIdList().add((long) (100000 + i));
        }

        String json = JSON.toJSONString(model.getMerchantIdList());
        byte[] bytes = json.getBytes();
        System.out.println("---------------------------  " + bytes.length);
        System.out.println("---------------------------  " + bytes.length / 1024 / 1024);
        return dynamicViewConfigMapper.insert2(model.getBatchId(), model.getReason(), json, model.getMerchantIdList().size());
    }

    @GetMapping("/test2")
    public Object test2(@RequestParam(value = "code") String code) {
        System.out.println("---------------------------  " + code);
        List<MerchantMarketErrorVO> list = dynamicViewConfigMapper.selectErrors();
        String json = JSON.toJSONString(list);
        byte[] bytes = json.getBytes();
        System.out.println("---------------------------  " + bytes.length);
        return list;
    }

    public void saveError(MerchantMarketErrorVO model) {
        // MerchantMarketErrorResult one = dynamicKpiColumnMapper.getByBatch(model.getBatchId(), model.getReason());
        // if (one == null) {
        // } else {
        // Set<Long> ids = new HashSet<>();
        // if (!StringUtils.isEmpty(one.getMerchantIdList())) {
        //     List<Long> merchantIdList = JSON.parseArray(one.getMerchantIds(), Long.class);
        //     ids.addAll(merchantIdList);
        // }
        // if (!CollectionUtils.isEmpty(model.getMerchantIdList())) {
        //     ids.addAll(model.getMerchantIdList());
        // }
        // dynamicKpiColumnMapper.update(one.getId(), JSON.toJSONString(ids), ids.size());
    }
}
